﻿using System;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.FileExpress.Controls
{
    public partial class FileUpload : AddEditItemControl
    {
        protected string GetFlashVars()
        {
            return "?" + this.Server.UrlEncode(this.Request.QueryString.ToString());
        }

        protected static string GetFlashPath()
        {
            return Global.ApplicationPath + "/Modules/FileExpress/FlashFileUpload.swf";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string jscript = "function UploadComplete(){";
            jscript += string.Format("__doPostBack('{0}','');", this.LinkButton1.ClientID.Replace("_", "$"));
            jscript += "};";
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "FileCompleteUpload", jscript, true);
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            this.DataList1.DataBind();
            //GridView1.DataSource = Directory.GetFiles(Server.MapPath("~/Uploads/FileExpress"));
            //GridView1.DataBind();
            // Do something that needs to be done such as refresh a gridView
            // say you had a gridView control called gvMyGrid displaying all 
            // the files uploaded. Refresh the data by doing a databind here.
            // gvMyGrid.DataBind();
        }

        protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            int ret = Components.Attachment.Delete(Convert.ToInt32(this.DataList1.DataKeys[e.Item.ItemIndex]));
            if (ret > 0)
            {
                string filename = this.Server.MapPath("~/Uploads/FileExpress/" + this.ModuleID + "/" + e.CommandArgument);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                this.DataList1.DataBind();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = string.Format("~/Default.aspx?tabindex={0}&tabid={1}&itemid={2}&mid={3}&page=AddEditPackage", this.TabIndex, this.TabID, this.ItemID, this.ModuleID);
            this.Response.Redirect(url);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            DataSet ds = Components.Attachment.SelectBy_PackageID(this.ItemID);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string filename = this.Server.MapPath("~/Uploads/FileExpress/" + this.ModuleID + "/" + row["FileName"]);
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
            }

            if (Components.Attachment.DeleteBy_PackageID(this.ItemID) > 0)
            {
                string url = string.Format("~/Default.aspx?tabindex={0}&tabid={1}&itemid={2}&mid={3}&page=AddEditPackage", this.TabIndex, this.TabID, this.ItemID, this.ModuleID);
                this.Response.Redirect(url);
            }
        }
    }
}